//菜的分类
var type = 101;
$(function () {
    findCaiType();
    findAllCai();
    findAlreadyOrder();
    var number = 1;
    $("#number_").click(function () {
        if (number == 1) {
            alert("不能小于1");
        } else {
            number--;
            $("#number").html(number);
        }
    });
    $("#number+").click(function () {
        number++;
        $("#number").html(number);
    });
    $("#save").click(function () {
        var id = $("#hideInput").val();
        $.post("../orderFood.do", {"id": id, "number": number}, function (data) {
            if (data == "ok") {
                $("#myModal").modal('hide');
                findAlreadyOrder();
            } else {
                alert("预订失败");
            }
        }, "json");
    })
});

//查询菜的种类
function findCaiType() {
    $("#topUl1").empty();
    $.post("../findAllCaiType.do", function (data) {
        for (var i = 0; i < data.length; i++) {
            var li = '<li id="\'' + data[i].classId + '\'" onclick="clickType(\'' + data[i].classId + '\')">' + data[i].className + '</li>'
            $("#topUl1").append(li);
        }
    }, "json")
}

//为菜的种类添加点击事件
function clickType(id) {
    type = id;//菜的分类id为当前点击
    findAllCai();
}

function findAllCai() {
    $("#dish").empty();
    $.ajaxSettings.async = false;
    $.post("../findAllCaiByType.do", {"type": type}, function (data) {
        for (var i = 0; i < data.length; i++) {
            var json = JSON.stringify(data[i]);
            var temp = encodeURI(json);
            var li = '<li><img onclick="openImg(\'' + temp + '\')" style="width: 240px;height: 180px" src="http://127.0.0.1:8081/upload/' + data[i].foodImage + '"/><h4>' + data[i].foodName + '</h4>\n' +
                '                <p class="price">' + data[i].foodPrice + '元/份</p>\n' +
                '                <p class="discount"><span>推荐星级</span>' + data[i].foodEvaluate + '</p>\n' +
                '                <a href="#" onclick="addMenu(\'' + temp + '\')" class="add"><img src="../img/add.png" /></a>\n' +
                '           </li>';
            $("#dish").append(li);
        }
    }, "json")
}

function addMenu(temp) {
    var json = decodeURI(temp);
    var obj = JSON.parse(json);
    $.post("../addMenu.do", {"foodId": obj.foodId}, function (data) {
        alert(data.message);
        findAlreadyOrder();
    }, "json");
}

function openImg(temp) {
    var json = decodeURI(temp);
    var obj = JSON.parse(json);
    $("#myModal").modal('show');
    $("#caiBigName").html(obj.foodName);
    $("#foodIntroduce").html(obj.foodDetail);
    $("#bigFoodPrice").html(obj.foodPrice);
    $("#hideInput").val(obj.foodId);
    $("#imgBigFood").attr("src", "http://127.0.0.1:8081/upload/" + obj.foodImage);
    const evaluate = obj.foodEvaluate;
    $("#uls").empty();
    var li = '<img src="../img/xingxing.png"/>';
    for (var i = 0; i < evaluate; i++) {
        $("#uls").append(li);
    }
}

function findAlreadyOrder() {
    $.post("../findAlreadyOrder.do", function (data) {
        $("#alreadyOrder").html(data);
    });
}